﻿using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using PlanningGIS.Framework;
using PlanningGIS.Win.Controls;
using ESRI.ArcGIS.ADF.BaseClasses;
namespace PlanningGIS.File
{
    /// <summary>
    /// 作用：地图打开类
    /// 作者：
    /// 编写日期:2007-08-01
    /// </summary>
    public class OpenMxd : BaseCommand
    {
        /// <summary>
        /// 
        /// </summary>
        IApplication m_Application = null;
        /// <summary>
        /// 
        /// </summary>
        public OpenMxd()
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hook"></param>
        public override void OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
              //  base.OnCreate((m_Application as IFramework).Object);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public override void OnClick()
        {
            if ((m_Application as IFramework).IsSceneCtlActive == false)
            {
                IMxDocument pMxDocment = (m_Application as ApplicationClass).MxDocument;
                if (pMxDocment != null && pMxDocment.FocusMap !=null && pMxDocment.FocusMap.LayerCount > 0)
                {
                    IMapDocument pMapDocment = (pMxDocment as MxDocumentClass).MapDocument;
                    if (pMapDocment != null )
                    {
                        DialogResult dr = DialogResult.Cancel;
                        try
                        {
                            if (pMapDocment.DocumentFilename == "")
                            {
                                dr = MsgHelper.ShowYesNoCancelQueryMsgbox("是否需要保存当前模板？");
                                if (dr == DialogResult.Yes)
                                {
                                    SaveAsMxd saveAsMxd = new SaveAsMxd();
                                    saveAsMxd.OnCreate(m_Application);
                                    saveAsMxd.OnClick();
                                }
                            }
                            else
                            {
                                dr = MsgHelper.ShowYesNoCancelQueryMsgbox("是否需要保存‘" + pMapDocment.DocumentFilename + "’？");
                                if (dr == DialogResult.Yes)
                                {
                                    m_Application.SaveDocument("");
                                }
                            }
                        }
                        catch
                        {
                            dr = MsgHelper.ShowYesNoCancelQueryMsgbox("是否需要保存当前模板？");
                            if (dr == DialogResult.Yes)
                            {
                                SaveAsMxd saveAsMxd = new SaveAsMxd();
                                saveAsMxd.OnCreate(m_Application);
                                saveAsMxd.OnClick();
                            }
                        }
                        if (dr == DialogResult.Cancel) 
                            return;
                    }
                }
            }                 
            OpenFileDialog dialog = new OpenFileDialog();
            if ((m_Application as IFramework).IsSceneCtlActive)
            {
                dialog.Filter = "sxd file(*.sxd)|*.sxd";
            }
            else
            {
                dialog.Filter = "mxd file(*.mxd)|*.mxd";
            }
            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                System.Windows.Forms.Application.DoEvents();              
                m_Application.OpenDocument(dialog.FileName);
                if ((m_Application as IFramework).IsSceneCtlActive==false && AppConfig.SystemConfig.鹰眼图.鹰眼类型 == "0")
                {
                    Control control = (m_Application as IFramework).DockManager.GetControlFromDockPanel("鹰眼图");
                    if (control == null) return;
                    PlanningGIS.Framework.Controls.OverViewPanel overViewPanel = control.Controls[0] as PlanningGIS.Framework.Controls.OverViewPanel;
                    if (overViewPanel == null) return;
                    overViewPanel.pEvent_OnMapReplaced(null);
                } 
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        public override bool Checked
        {
            get
            {
                if (m_Application == null) return false; 
                return base.Checked;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override string Name
        {
            get { return base.Name; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override string Caption
        {
            get { return "打开工作区"; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override string Tooltip
        {
            get { return "打开工作区"; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override string Message
        {
            get { return "打开工作区"; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override string HelpFile
        {
            get { return base.HelpFile; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override int HelpContextID
        {
            get { return base.HelpContextID; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override int Bitmap
        {
            get { return base.Bitmap; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override string Category
        {
            get { return base.Category; }
        }

        /// <summary>
        /// 是否可用
        /// </summary>
        public override bool Enabled
        {
            get
            {
                if (m_Application == null)
                    return false; 
                return (m_Application as IFramework).IsMapCtrlactive || (m_Application as IFramework).IsSceneCtlActive;                        
            }
        }
    }
}